package com.graduation.roombooking.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.roombooking.dto.SysCompanyDTO;
import com.graduation.roombooking.exception.GlobalException;
import com.graduation.roombooking.mapper.CompanyMapper;
import com.graduation.roombooking.pojo.Company;
import com.graduation.roombooking.service.ICompanyService;
import com.graduation.roombooking.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoXinYi
 * @since 2022-04-30
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public RespBean getAll() {
        return RespBean.success(companyMapper.getAll());
    }

    @Override
    public RespBean companyDropDownBox() {
        return RespBean.success(companyMapper.companyDropDownBox());
    }

    @Override
    public RespBean unionQuery(CompanyUnionQueryVo companyUnionQueryVo) {
        return RespBean.success(companyMapper.unionQuery(companyUnionQueryVo));
    }

    @Override
    public RespBean updateCompany(UpdateCompanyVo updateCompanyVo) {
        int row = companyMapper.updateCompany(updateCompanyVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"修改失败，请检查数据合法性");
        }
        return RespBean.success();
    }

    @Override
    public RespBean registerDepartment(RegisterCompanyVo registerCompanyVo) {
        //判断部门是否注册过
        SysCompanyDTO flag=companyMapper.getCompanyByName(registerCompanyVo.getName());
        if(null!=flag){
            return RespBean.error(RespBeanEnum.COMPANY_REPEAT);
        }


        Company company=new Company();
        company.setName(registerCompanyVo.getName());
        company.setAddress(registerCompanyVo.getAddress());
        company.setDescription(registerCompanyVo.getDescription());
        company.setLeaderPhone(registerCompanyVo.getLeaderPhone());
        company.setTelephone(registerCompanyVo.getTelephone());
        company.setHead(registerCompanyVo.getHead());
        company.setLongitude(registerCompanyVo.getLongitude());
        company.setLatitude(registerCompanyVo.getLatitude());

        try {
            if (companyMapper.addOne(company)<1) {
                return RespBean.error(RespBeanEnum.UPDATE_ERROR,"创建失败");
            }
        } catch (Exception e) {
            throw new GlobalException(RespBeanEnum.BIN_ERROR);
        }
        return RespBean.success("注册成功！");
    }

    @Override
    public RespBean getCompanyByName(String companyName) {
        return RespBean.success(companyMapper.getCompanyByName(companyName));
    }

    @Override
    public RespBean getAppletCompanyList(AppletCompanyVO appletCompanyVO) {
        return RespBean.success(companyMapper.getAppletCompanyList(appletCompanyVO));
    }
}
